var wxpay = require('../../utils/pay.js')
var app = getApp()
Page({
  data:{
    fileServerPath: app.globalData.fileServerPath,
    statusType: [
      {name:"全部",currentStatus:""},
      { name: "待付款", currentStatus: "NotPay"},
      { name: "待发货", currentStatus: "WaitGoods"},
      { name: "待收货", currentStatus: "ReceiveGoods"},
      { name: "已完成", currentStatus: "Complete"},
      { name: "已取消", currentStatus: "Cancelled"}
    ],
    currentType:"",
    tabClass: ["", "NotPay", "WaitGoods", "ReceiveGoods", "Complete","Cancelled"],
    fileter: function (data) {
      if(data){
        return data.replace(/\|/, ",")
      }
      else{
        return ""
      }
    }
  },
  statusTap:function(e){
    var curType = e.currentTarget.dataset.status;
     this.data.currentType = curType
     this.setData({
       currentType:curType
     });
     this.onShow();
  },
  togoods:function(e){
    wx.navigateTo({
      url: "/pages/goods-details/index?id=" + e.currentTarget.dataset.goodsid
    })
  },
  orderDetail : function (e) {
    var orderId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/pages/order-details/index?id=" + orderId
    })
  },
  cancelOrderTap:function(e){
    var that = this;
    var orderId = e.currentTarget.dataset.id;
     wx.showModal({
      title: '确定要取消该订单吗？',
      content: '',
      success: function(res) {
        if (res.confirm) {
          wx.showLoading();
          wx.request({
            url: app.globalData.rootPath + '/m/order/cancel',
            data: {
              token: wx.getStorageSync('token'),
              orderId: orderId
            },
            success: (res) => {
              wx.hideLoading();
              if (res.data.meta.code == 200) {
                that.onShow();
              }else{
                wx.showModal({
                  title: '提示',
                  content: '请求出错--'+res.data.meta.message,
                  showCancel: false
                })
              }
            }
          })
        }
      }
    })
  },
  toPayTap:function(e){
    var that = this;
    var orderId = e.currentTarget.dataset.id;
    wx.request({
      url: app.globalData.rootPath + '/m/order/pay',
      data: {
        token: wx.getStorageSync('token'),
        orderId: orderId
      },
      success: (res) => {
        if(res.data.meta.code === 200){
          wx.requestPayment({
            'timeStamp': res.data.data.timeStamp,
            'nonceStr': res.data.data.nonceStr,
            'package': res.data.data.package1,
            'signType': 'MD5',
            'paySign': res.data.data.paySign,
            fail: function (aaa) {
              if (aaa.errMsg == "requestPayment:fail cancel") {
                wx.showModal({
                  title: '提示',
                  content: "支付已取消，您可以在30分钟内完成支付。",
                  showCancel: false
                })
              } else {
                wx.showModal({
                  title: '提示',
                  content: aaa.errMsg,
                  showCancel: false
                })
              }
            },
            success: function (res) {
              that.onShow();
            }
          })
        }else{
          wx.showModal({
            title: '提示',
            content: res.data.meta.message,
            showCancel: false
          })
        }
      }
    })
  },
  orderConfirm: function (e){
    var that = this;
    var orderId = e.currentTarget.dataset.id;
    wx.request({
      url: app.globalData.rootPath + '/m/order/confirm',
      data: {
        token: wx.getStorageSync('token'),
        orderId: orderId
      },
      success: (res) => {
        wx.hideLoading();
        if (res.data.meta.code == 200) {
          that.onShow();
        } else {
          wx.showModal({
            title: '提示',
            content: '请求出错--' + res.data.meta.message,
            showCancel: false
          })
        }
      }
    })
  },

  onShow:function(){
    // 获取订单列表
    wx.showLoading();
    var that = this;
    wx.request({
      url: app.globalData.rootPath+'/m/order',
      data: {
        token: wx.getStorageSync('token'),
        status: that.data.currentType
      },
      success: (res) => {
        wx.hideLoading();
        if (res.data.meta.code === 200) {
          that.setData({
            orderList: res.data.data,
          });
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.meta.message,
            showCancel: false
          })
         
          this.setData({
            orderList: null,
            logisticsMap: {},
            goodsMap: {}
          });
        }
      }
    })
    
  }
})